$(function () {
    //1.抽取变量
    var expend_datagrid = $("#expend_datagrid");
    var expend_dialog = $("#expend_dialog");
    var expend_point_dialog = $("#expend_point_dialog");
    var expend_form = $("#expend_form");
    var expend_form_piont = $("#expend_form_piont");
    var expend_btns = $("#expend_btns");
    var expendPoint_btns = $("#expendPoint_btns");

    //数据表格
    expend_datagrid.datagrid({
        toolbar: "#expend_toolbar",
        fitColumns: true,
        striped:true

    });

    //弹框
    expend_dialog.dialog({
        width: 300,
        height: 200,
        buttons: "#expend_btns",
        closed: true,
        onClose: function () {
            //清空表单
            expend_form.form('clear');
        }
    });
    //弹框
    expend_point_dialog.dialog({
        width: 300,
        height: 200,
        left: 150,
        top:150,
        buttons: '#expendpoint_btns',
        closed: true,
        onClose: function () {
            //清空表单
            expend_form.form('clear');
        }
    })

    //2.使用一个对象来统一管理事件的方法
    var methodObj = {

        //新增分类支出
        add: function () {
            //清空表单数据
            // expend_form.form("clear");
            //打开弹出框
            expend_dialog.dialog('open');
            expend_dialog.dialog('setTitle', '新增分类');
        },

        //新增子类支出
        addpoint: function (obj) {
            //打开弹出框
            expend_point_dialog.dialog("open");
            expend_point_dialog.dialog("setTitle", "新增分类");
        },

        save: function () {
            //提交表单
            expend_form.form('submit', {
                url: '/expend/saveOrUpdate.do',
                onSubmit: function (param) {
                    //获取文本框的值
                    var name = $("#expendText").textbox("getValue");
                    param["name"] = name;
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        //提示用户操作结果
                        $.messager.alert('温馨提示', '保存成功!', 'info', function () {
                            methodObj.cancel();
                            window.location.reload();
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },
        point_save: function () {
            //提交表单
            expend_form_piont.form('submit', {
                url: '/expend/saveOrUpdate.do',
                onSubmit: function (param) {
                    //获取文本框的值
                    var name = $("#expendPointText").textbox("getValue");
                    param["name"] = name;
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        //提示用户操作结果
                        $.messager.alert('温馨提示', '保存成功!', 'info', function () {
                            expend_point_dialog.dialog("close");
                            methodObj.cancel();
                            window.location.reload();
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },
        //刷新
        reload: function () {
            // expend_datagrid.datagrid("reload");
            window.location.reload();
        },
        //取消
        cancel: function () {
            expend_dialog.dialog('close');
        },

        //取消
        point_cancel: function () {
            expend_point_dialog.dialog('close');
        },
    }

    //3.统一绑定事件
    $("a[data-cmd]").click(function () {
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })

})

//添加子分类
function addpoint(obj) {
    var expend_point_dialog = $("#expend_point_dialog");
    var expend_form_piont = $("#expend_form_piont");
    (function () {
        //清空表单数据
        expend_form_piont.form("clear");
        //设置标题
        expend_point_dialog.dialog("setTitle", "支出子分类");
        //把获取的父id赋值到表单的parent.id
        $("#parent_id").val(obj.id);
        //打开弹窗
        expend_point_dialog.dialog("open");
    })();
}

//删除父支出分类
function delete1(obj) {
    //弹出确认框
    $.messager.confirm('温馨提示', '真的删除吗?', function (r) {
        if (r) {
            $.get("/expend/delete.do", {id: obj.id}, function (data) {
                if (data.success) {
                    $.messager.alert('温馨提示', "删除成功", 'info', function () {
                        window.location.reload();
                    });
                } else {
                    $.messager.alert('温馨提示', data.msg, 'error');
                }
            }, "json")
        }
    })
}


